//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "HKDCurrency.h"
using namespace Cephei::QL::Currencies;
#include <gen/QL/Math/Rounding.h>
#include <gen/QL/Currency.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Currencies::CHKDCurrency::CHKDCurrency () : CCurrency(CHKDCurrency::typeid)
{
    try
    {
#ifdef HANDLE
        _phHKDCurrency = NULL;
#endif
        _ppHKDCurrency = new boost::shared_ptr<QuantLib::HKDCurrency> (new QuantLib::HKDCurrency ( ));
        SetCurrency (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppHKDCurrency));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Currencies::CHKDCurrency::CHKDCurrency (boost::shared_ptr<QuantLib::HKDCurrency>& childNative, Object^ owner) : CCurrency(CHKDCurrency::typeid)
{
#ifdef HANDLE
	_phHKDCurrency = NULL;
#endif
	_ppHKDCurrency = &childNative;
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppHKDCurrency));
}
Cephei::QL::Currencies::CHKDCurrency::CHKDCurrency (QuantLib::HKDCurrency& childNative, Object^ owner) : CCurrency(CHKDCurrency::typeid)
{
#ifdef HANDLE
	_phHKDCurrency = NULL;
#endif
	_ppHKDCurrency = new boost::shared_ptr<QuantLib::HKDCurrency> (&childNative);
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppHKDCurrency));
    _HKDCurrencyOwner = owner;
    _CurrencyOwner = owner;
}

Cephei::QL::Currencies::CHKDCurrency::CHKDCurrency (CHKDCurrency^ copy) : CCurrency(CHKDCurrency::typeid)
{
#ifdef HANDLE
	_phHKDCurrency = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppHKDCurrency = new boost::shared_ptr<QuantLib::HKDCurrency> (copy->GetShared());
        _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppHKDCurrency));
    }
}
Cephei::QL::Currencies::CHKDCurrency::CHKDCurrency (PLATFORM::Type^ t) : CCurrency(CHKDCurrency::typeid)
{
#ifdef HANDLE
	_phHKDCurrency = NULL;
#endif
	if (!t->IsSubclassOf(CHKDCurrency::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Currencies::CHKDCurrency::CHKDCurrency (QuantLib::Handle<QuantLib::HKDCurrency>& childNative, Object^ owner)  : CCurrency(CHKDCurrency::typeid)
{
	_phHKDCurrency = &childNative;
	_ppHKDCurrency = &static_cast<boost::shared_ptr<QuantLib::HKDCurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppHKDCurrency));
    _HKDCurrencyOwner = owner;
}
Cephei::QL::Currencies::CHKDCurrency::CHKDCurrency (QuantLib::Handle<QuantLib::HKDCurrency> childNative)  : CCurrency(CHKDCurrency::typeid)
{
	_phHKDCurrency = &childNative;
	_ppHKDCurrency = &static_cast<boost::shared_ptr<QuantLib::HKDCurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppHKDCurrency));
}
#endif
#ifdef STRUCT
Cephei::QL::Currencies::CHKDCurrency::CHKDCurrency (QuantLib::HKDCurrency childNative)  : CCurrency(CHKDCurrency::typeid)
{
#ifdef HANDLE
	_phHKDCurrency = NULL;
#endif
	_ppHKDCurrency = new boost::shared_ptr<QuantLib::HKDCurrency> (new QuantLib::HKDCurrency (childNative));
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppHKDCurrency));
}
#endif

Cephei::QL::Currencies::CHKDCurrency::~CHKDCurrency ()
{
    if (_ppHKDCurrency != NULL)
    {
	    delete _ppHKDCurrency;
        _ppHKDCurrency = NULL;
    }
}
Cephei::QL::Currencies::CHKDCurrency::!CHKDCurrency ()
{
    if (_ppHKDCurrency != NULL)
    {
	    delete _ppHKDCurrency;
    }
}
QuantLib::HKDCurrency& Cephei::QL::Currencies::CHKDCurrency::GetReference ()
{
    if (_ppHKDCurrency == NULL) throw REFNEW NativeNullException ();
	return **_ppHKDCurrency;
}
boost::shared_ptr<QuantLib::HKDCurrency>& Cephei::QL::Currencies::CHKDCurrency::GetShared ()
{
    if (_ppHKDCurrency == NULL) throw REFNEW NativeNullException ();
	return *_ppHKDCurrency;
}
QuantLib::HKDCurrency* Cephei::QL::Currencies::CHKDCurrency::GetPointer ()
{
    if (_ppHKDCurrency == NULL) throw REFNEW NativeNullException ();
	return &**_ppHKDCurrency;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::HKDCurrency>& Cephei::QL::Currencies::CHKDCurrency::GetHandle ()
{
	if (_phHKDCurrency == NULL)
	{
		_phHKDCurrency = new Handle<QuantLib::HKDCurrency> (*_ppHKDCurrency);
	}
	return *_phHKDCurrency;
}
#endif
bool Cephei::QL::Currencies::CHKDCurrency::HasNative () 
{
	return (_ppHKDCurrency != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Currencies::IHKDCurrency^ Cephei::QL::Currencies::CHKDCurrency_Factory::Create ()
{
    return REFNEW CHKDCurrency ();
}
